package me.yricky.mayu.ui.tasks.taskui.seqali

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material.icons.rounded.AlignHorizontalLeft
import androidx.compose.material.icons.rounded.Done
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import me.yricky.mayu.desktop.AppState
import me.yricky.mayu.ui.GridItemButton
import me.yricky.mayu.ui.NumberSelector
import me.yricky.mayu.ui.fasta.DataContentItem
import me.yricky.mayu.ui.fasta_box.Page
import me.yricky.mayu.ui.fasta.FastaSeqModel
import me.yricky.mayu.utils.sequence.SequenceAlignmentSession
import java.awt.Dimension

/**
 * @author Yricky
 * @date 2022/4/25
 */


@Composable
fun SeqAlignmentTaskEntry(modifier: Modifier = Modifier.fillMaxWidth().height(160.dp)){
    var showDialog by remember { mutableStateOf(false) }
    GridItemButton({
        showDialog = true
    },modifier,Icons.Rounded.AlignHorizontalLeft){
        Text("序列比对")
    }
    SeqAlignmentTaskDialog(showDialog){ showDialog = false }
}

@Composable
fun SeqAlignmentTaskDialog(showDialog:Boolean,onClose:()->Unit) = Dialog(onClose, visible = showDialog, title = "新建序列比对任务", icon = rememberVectorPainter(Icons.Rounded.Add)){
    LaunchedEffect(Unit){
        window.minimumSize = Dimension(800,600)
    }
    var seq1: FastaSeqModel? by remember { mutableStateOf(null) }
    var seq2: FastaSeqModel? by remember { mutableStateOf(null) }
    var matchScore by remember { mutableStateOf(3) }
    var mismatchScore by remember { mutableStateOf(-3) }
    var vScore by remember { mutableStateOf(-2) }

    Scaffold(
        floatingActionButton = {
            if(((seq1?.seq?.size ?: 0) > 0) && ((seq2?.seq?.size ?: 0) > 0)){
                FloatingActionButton({
                    AppState.tasksListModel.list.add(SeqAlignmentTask(seq1!!,seq2!!,
                        SequenceAlignmentSession.simpleMatchPairScore(matchScore, mismatchScore),vScore))
                    onClose()
                }){ Icon(Icons.Rounded.Done,"") }
            }
        }
    ) { paddingValue ->
        var content:@Composable (()->Unit)? by remember { mutableStateOf(null) }

        val seq1Content:@Composable ()->Unit = remember { {
            AppState.fastaBoxesModel.Page {
                seq1 = it
                content = null
            }
        } }
        val seq2Content:@Composable ()->Unit = remember { @Composable {
            AppState.fastaBoxesModel.Page {
                seq2 = it
                content = null
            }
        } }
        Box(Modifier.padding(paddingValue)){
            if(content != null){
                content?.invoke()
            }else{
                Column(
                    Modifier.verticalScroll(rememberScrollState())
                ) {
                    Row{
                        Column(Modifier.weight(1f), horizontalAlignment = Alignment.CenterHorizontally) {
                            Text("匹配得分")
                            NumberSelector(Modifier.padding(8.dp),value = matchScore, valueRange = -10 .. 10){
                                matchScore = it

                            }
                        }
                        Column(Modifier.weight(1f), horizontalAlignment = Alignment.CenterHorizontally) {
                            Text("错配罚分")
                            NumberSelector(Modifier.padding(8.dp),value = mismatchScore, valueRange = -10 .. 10){
                                mismatchScore = it
                            }
                        }
                        Column(Modifier.weight(1f), horizontalAlignment = Alignment.CenterHorizontally) {
                            Text("空位罚分")
                            NumberSelector(Modifier.padding(8.dp),value = vScore, valueRange = -10 .. 10){
                                vScore = it
                            }
                        }
                    }

                    Button({content = seq1Content}){
                        Text("选择序列1")
                    }
                    seq1?.DataContentItem()
                    Button({content = seq2Content}){
                        Text("选择序列2")
                    }
                    seq2?.DataContentItem()
                }
            }
        }
    }
}